package com.acme.entity;

import com.acme.entity.util.ChangeSupportList;
import com.acme.entity.util.EntityBean;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.swing.event.ChangeListener;

/**
 *
 * @author alejandro
 */
@Entity
@Table(name = "POBLADO", catalog = "carreteras", schema = "dbo")
@NamedQueries({
    @NamedQuery(name = "Poblado.findAll", query = "SELECT p FROM Poblado p"),
    @NamedQuery(name = "Poblado.findById", query = "SELECT p FROM Poblado p WHERE p.id = :id"),
    @NamedQuery(name = "Poblado.findByNombre", query = "SELECT p FROM Poblado p WHERE p.nombre = :nombre"),
    @NamedQuery(name = "Poblado.findByKilometo", query = "SELECT p FROM Poblado p WHERE p.kilometo = :kilometo")})
public class Poblado extends EntityBean<Poblado> {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "nombre", nullable = false, length = 50)
    private String nombre;
    @Basic(optional = false)
    @Column(name = "kilometo", nullable = false, length = 15)
    private String kilometo;
    @JoinTable(name = "POBLADO_SERVICIO", joinColumns = {
        @JoinColumn(name = "poblado", referencedColumnName = "id", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "servicio", referencedColumnName = "id", nullable = false)})
    @ManyToMany
    private final ChangeSupportList<Servicio> servicioList = new ChangeSupportList<Servicio>();
    @JoinColumn(name = "tramo", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Tramo tramo;
    @JoinColumn(name = "tipo", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private TipoPoblado tipoPoblado;

    public Poblado() {
    }

    public Poblado(Integer id) {
        this.id = id;
    }

    public Poblado(Integer id, String nombre, String kilometo) {
        this.id = id;
        this.nombre = nombre;
        this.kilometo = kilometo;
    }

    public Poblado(Integer id, String nombre, String kilometo, List<Servicio> servicioList, Tramo tramo, TipoPoblado tipoPoblado) {
        this.id = id;
        this.nombre = nombre;
        this.kilometo = kilometo;
        mergeListContent(this.servicioList, servicioList);
        this.tramo = tramo;
        this.tipoPoblado = tipoPoblado;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        support.fireChange();
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
        support.fireChange();
    }

    public String getKilometo() {
        return kilometo;
    }

    public void setKilometo(String kilometo) {
        this.kilometo = kilometo;
        support.fireChange();
    }

    public List<Servicio> getServicioList() {
        return servicioList;
    }

    public void setServicioList(List<Servicio> servicioList) {
        mergeListContent(this.servicioList, servicioList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public Tramo getTramo() {
        return tramo;
    }

    public void setTramo(Tramo tramo) {
        this.tramo = tramo;
        support.fireChange();
    }

    public TipoPoblado getTipoPoblado() {
        return tipoPoblado;
    }

    public void setTipoPoblado(TipoPoblado tipoPoblado) {
        this.tipoPoblado = tipoPoblado;
        support.fireChange();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Poblado)) {
            return false;
        }
        Poblado other = (Poblado) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return String.format("%s km%s (%s)", nombre, kilometo, tramo);
    }

    @Override
    public void addChangeListener(ChangeListener listener) {
        servicioList.addChangeListener(listener);
        super.addChangeListener(listener);
    }

    @Override
    public Poblado clone() {
        return new Poblado(id, nombre, kilometo, servicioList, tramo, tipoPoblado);
    }

    @Override
    public void merge(Poblado other) {
        this.id = other.id;
        this.nombre = other.nombre;
        this.kilometo = other.kilometo;
        mergeListContent(this.servicioList, other.servicioList);
        this.tramo = other.tramo;
        this.tipoPoblado = other.tipoPoblado;
    }

}
